<template>
  <div>
    <el-dialog title="收货地址" :visible.sync="classfyadd.flag">
      {{ form }}
      <el-form :model="form">
        <el-form-item label="上级分类" :label-width="formLabelWidth">
          <el-select placeholder="请选择分类" v-model="form.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in classfylist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :on-change="changeimg"
            :show-file-list="false"
            :auto-upload="false"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="分类状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="classfyadd.flag = false">取 消</el-button>
        <el-button type="primary" @click="addclassfy">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["classfyadd", "classfylist"],
  data() {
    return {
      formLabelWidth: "120px",
      form: {
        catename: "",
        pid: "",
        img: "",
        status: 1,
      },
      value: 1,
      imageUrl: "",
    };
  },
  computed: {},
  created() {},
  mounted() {},
  watch: {
    classfyadd: {
      // 弹出框状态监听
      deep: true,
      handler: function (newval) {
        if (newval.flag == false) {
          this.form = {
            catename: "",
            pid: "",
            img: "",
            status: 1,
          };
          this.imageUrl = ''
        }
      },
    },
  },
  methods: {
    changeimg(file) {
      // 图片文件状态改变时的回调
      if (file.size > 1024 * 1024 * 2) {
        this.$message({
          message: "图片的大小不能超过2MB,请重新选择",
          type: "warning",
        });
        return;
      }
      let urlArr = ["jpg", "png", "jpeg", "svg", "gif"];
      let url = file.name.split(".").pop();
      if (!urlArr.includes(url)) {
        this.$message({
          message: "您选择的图片不符合规范,请重新选择",
          type: "warning",
        });
        return;
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
    addclassfy() {
      console.log(this.form);
      // 确认事件
      if (this.form.catename == "") {
        // 判空
        this.$message.error("请输入正确的分类名称");
        return;
      } else {
        (this.form.id
          ? this.$api.postcateedit(this.form)
          : this.$api.postcateadd(this.form)
        ).then((res) => {
          this.$message({
            message: "恭喜你，这是一条成功消息",
            type: "success",
          });
          this.$emit("getclassfyList");
        });
      }
      this.classfyadd.flag = false;
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
